/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmSelectCurso.java
 *
 * Created on Jun 16, 2010, 5:50:09 PM
 */

package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.Decorator.ConcretDecorator;
import br.edu.sistemaacademico.adapter.IMenuCadastro;
import br.edu.sistemaacademico.pojo.Curso;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

/**
 *
 * @author baron
 */
public class frmSelectCurso extends IMenuCadastro {
	ConcretDecorator cc = null;
	StateHandler stateAlterarExcuir = new StateHandler(false);
    public List<Object> campos = ClientCommand.getCampos("CursoService"); //new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    TabelaModelo tabModel;
    List<Curso> extra;
    Curso curso;
    frmCadastroTurma frmt;
    
    public frmSelectCurso(frmCadastroTurma frmt) {
    	initComponents();
    	tamanho.add("50");
    	tabModel = new TabelaModelo(campos, tamanho, jTable1);
    	ListSelectionModel listModel = jTable1.getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        if (cc==null)
            cc = new ConcretDecorator(this);
        this.frmt = frmt;
        setButtons();
        setStateAlteraExcuir();
        atualiza();
        cc.setVisible(true);
    }
    	
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(525, 357));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("jButton1");
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setText("jButton2");
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(206, 206, 206)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
		
		  public void atualiza(){
		    	((DefaultTableModel) jTable1.getModel()).setNumRows(0);
		    	jTable1.updateUI();
		    	extra = (List) ClientCommand.carregar("CursoService","*", "");
		    	System.out.println(extra.size());
		    	for(int i = 0; i < extra.size();i++ ){
		    		valores = new LinkedList<Object>();
		    		valores.add(extra.get(i).getId());
		    		valores.add(extra.get(i).getNome());
		    		valores.add(extra.get(i).getAnoRegulamentado());
		    		tabModel.setValores(valores);
		    		valores.clear();
		    	}
		    }
		  
		  public void setButtons(){
			  jButton1.setText("Cancelar");
			  jButton1.setText("Adicionar");
			  jButton2.setActionCommand("adicionar");
			  jButton2.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					frmt.setCuso(curso);		
				}
			});
		  }
		  public Curso execute(){
			  return curso;
		  }
		  ListSelectionListener list = new ListSelectionListener() {
	    		
	    		@Override
	    		public void valueChanged(ListSelectionEvent e) {
	    			try{
	    			 curso = getSelectedCurso(Long.parseLong(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString()));
	    			}catch(ArrayIndexOutOfBoundsException ea){
	    				curso = null;
	    			}
	    		}
	    	};	
	    	public Curso getSelectedCurso(Long codigo){
	    		return (Curso) ClientCommand.carregaObjeto("CursoService", "codigo="+codigo, "");
	    	}
	        public void setStateAlteraExcuir(){
	        	stateAlterarExcuir.addComponent(jButton1);
	        }
	        public void limpaFields(){
	 	    }

	 	    
	 		@Override
	 		public void menuExemplo(JMenuItem item) {
	 			// TODO Auto-generated method stub
	 			
	 		}

	 		@Override
	 		public void menuFechar(JMenuItem item) {
	 			cc.dispose();		
	 		}

	 		@Override
	 		public void menuLimpa(JMenuItem item) {
	 			limpaFields();		
	 		}

	 		@Override
	 		public void menuManual(JMenuItem item) {
	 			// TODO Auto-generated method stub		
	 		}

	 		@Override
	 		public void menuMementor(JMenuItem item) {
	 			// TODO Auto-generated method stub		
	 		}

	 		@Override
	 		public void menuOperacao(JMenuItem item) {
	 			item.setText("Fechar Turma");
	 			//finalizar();
	 		}

	 		@Override
	 		public void menuSobre(JMenuItem item) {
	 			// TODO Auto-generated method stub
	 			
	 		}
}
